﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WSS.hameduconn.Bases;
using BussinessLogic;
using DataAccess;

namespace WSS.hameduconn
{
    public partial class AdminProfile : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsAuthenticated || AuthenticateInfo.admin == null)
                Response.Redirect(ResolveUrl("~/Login.aspx"));

            if (!IsPostBack)
            {
                tbConfirmPassword.Text = new AUEncrypt().Decrypt(AuthenticateInfo.admin.Password);
                tbUsername.Text = AuthenticateInfo.admin.Username;
                tbFirstName.Text = AuthenticateInfo.admin.Firstname;
                tbLastName.Text = AuthenticateInfo.admin.Lastname;
                tbPassword.Text = new AUEncrypt().Decrypt(AuthenticateInfo.admin.Password);
                tbJob.Text = AuthenticateInfo.admin.Jobtitle;
                memberSection.Visible = true;

                btnUpdate.Visible = true;
                divAlert.Visible = false;
            }
        }

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string fName = tbFirstName.Text;
            string lName = tbLastName.Text;
            string passWord = tbPassword.Text;
            string confirmPassWord = tbConfirmPassword.Text;
            string username = tbUsername.Text;
            string jobTitle = tbJob.Text;

            if (string.IsNullOrEmpty(username) || string.IsNullOrEmpty(passWord) || string.IsNullOrEmpty(confirmPassWord))
            {
                AlertWarning("Please input require field");
                return;
            }

            if (passWord != confirmPassWord)
            {
                AlertWarning("Confirm password incorrect");
                return;
            }

            Admin admin = new Admin
            {
                Firstname = fName,
                Lastname = lName,
                Jobtitle = jobTitle,
                Password = new AUEncrypt().Encrypt(passWord),
                Id = AuthenticateInfo.admin.Id
            };

            var adminControls = new BLAdmin();

            if (adminControls.Update(admin))
            {
                AlertSuccess("Update admin profile success !");
            }
            else
            {
                AlertError("Update admin profile fail!");
            }
        }

        protected void AlertWarning(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-danger";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void AlertError(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-danger";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }

        protected void AlertSuccess(string msg)
        {
            divAlert.Attributes["class"] = "alert alert-success";
            divAlert.InnerHtml = msg;
            divAlert.Visible = true;
        }
    }
}